using UnityEngine;

namespace SimpleFramework
{
    public static class UILayerPanel
    {
        private static RectTransform s_Root;
        private static RectTransform Root
        {
            get
            {
                if (s_Root == null)
                {
                    s_Root = GameObject.Find("Canvas").GetComponent<RectTransform>();
                }
                return s_Root;
            }
        }
        private static RectTransform[] Layers = null;

        public static RectTransform GetLayer(UILayer layerId)
        {
            if (Layers == null)
            {
                var names = System.Enum.GetNames(typeof(UILayer));
                Layers = new RectTransform[names.Length];
                for (int i = 0; i < names.Length; ++i)
                {
                    var layer = new GameObject().AddComponent<RectTransform>();
                    layer.name = names[i];
                    layer.SetParent(Root);
                    layer.localScale = Vector3.one;
                    layer.anchorMin = Vector2.zero;
                    layer.anchorMax = Vector2.one;
                    layer.offsetMin = Vector2.zero;
                    layer.offsetMax = Vector2.zero;
                    Layers[i] = layer;
                }
            }
            return Layers[(int)layerId];
        }

        public static void SetLayer(RectTransform target, UILayer layer, bool isFull)
        {
            var parent = GetLayer(layer);
            SetParent(target, parent, isFull);
        }

        public static void SetParent(RectTransform target, RectTransform parent, bool isFull)
        {
            target.SetParent(parent);

            if (isFull)
            {
                target.localScale = Vector3.one;
                target.anchorMin = Vector2.zero;
                target.anchorMax = Vector2.one;
                target.offsetMin = Vector2.zero;
                target.offsetMax = Vector2.zero;
            }
        }
    }
}